package com.united.ucts.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.united.frame.common.enums.YesOrNoEnum;
import com.united.frame.common.module.pojo.dto.BaseTreeDto;
import com.united.frame.common.module.service.impl.BaseServiceImpl;
import com.united.ucts.common.constant.AdminConstant;
import com.united.ucts.modules.system.mybatis.mapper.MenuMapper;
import com.united.ucts.modules.system.pojo.po.Menu;
import com.united.ucts.modules.system.service.MenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 后台菜单管理Service实现类
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<MenuMapper, Menu> implements MenuService {


    @Override
    public int create(Menu menu) {
        menu.setCreateTime(LocalDateTime.now());
        updateLevel(menu);
        return this.baseMapper.insert(menu);
    }

    /**
     * 修改菜单层级
     */
    private void updateLevel(Menu menu) {
        if (YesOrNoEnum.YES.getCode().equals(menu.getParentId())) {
            //没有父菜单时为一级菜单
            menu.setLevel(0);
        } else {
            //有父菜单时选择根据父菜单level设置
            Menu parentMenu = this.getById(menu.getParentId());
            if (parentMenu != null) {
                menu.setLevel(parentMenu.getLevel() + 1);
            } else {
                menu.setLevel(0);
            }
        }
    }


    @Override
    public int delete(String id) {
        return baseMapper.deleteById(id);
    }

    @Override
    public int update(String id, Menu menu) {
        menu.setId(id);
        updateLevel(menu);
        return baseMapper.updateById(menu);
    }

    @Override
    public Menu getItem(String id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Menu> pageBy(Integer pageNum, Integer pageSize, String parentId) {
        PageHelper.startPage(pageNum, pageSize);

        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Menu::getParentId, parentId);
        queryWrapper.orderByDesc(Menu::getSort);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BaseTreeDto> trees() {
        List<Menu> menus = this.list();

        return menus.stream()
                .filter(menu -> AdminConstant.ADMIN_ROOT_MENU_ID.equals(menu.getParentId()))
                .map(menu -> covertMenuNode(menu, menus))
                .collect(Collectors.toList());
    }

    @Override
    public int updateHidden(String id, Integer hidden) {
        Menu menu = new Menu();
        menu.setId(id);
        menu.setHidden(hidden);

        return this.baseMapper.updateById(menu);
    }

    /**
     * 将UmsMenu转化为UmsMenuNode并设置children属性
     */
    private BaseTreeDto covertMenuNode(Menu menu, List<Menu> menus) {
        BaseTreeDto node = new BaseTreeDto();
        BeanUtils.copyProperties(menu, node);

        // 得到子菜单
        List<BaseTreeDto> children = menus.stream()
                .filter(subMenu -> subMenu.getParentId().equals(menu.getId()))
                .map(subMenu -> covertMenuNode(subMenu, menus)).collect(Collectors.toList());
        node.setChildren(children);
        return node;
    }
}